package com.example.digitaldiary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class SmileyChooserActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smiley_chooser);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));

		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				/*Toast.makeText(SmileyChooserActivity.this, "" + arg2,
						Toast.LENGTH_SHORT).show();*/
				Intent data = new Intent();

				data.setData(Uri.parse("" + ImageAdapter.mSmileyIds[arg2]));
				
				setResult(RESULT_OK, data);
				finish();
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_smiley_chooser, menu);
		return true;
	}
}

class ImageAdapter extends BaseAdapter {

	private Context mContext;

	public ImageAdapter(Context context) {
		mContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mSmileyIds.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;

		if (convertView == null) {
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}
		imageView.setImageResource(mSmileyIds[position]);
		return imageView;
	}

	public static final Integer[] mSmileyIds = { R.drawable.popo01,
			R.drawable.popo02, R.drawable.popo03, R.drawable.popo04,
			R.drawable.popo05, R.drawable.popo06, R.drawable.popo07,
			R.drawable.popo08, R.drawable.popo09, R.drawable.popo10,
			R.drawable.popo11, R.drawable.popo12, R.drawable.popo13,
			R.drawable.popo14, R.drawable.popo15, R.drawable.popo16,
			R.drawable.popo17, R.drawable.popo18, R.drawable.popo19,
			R.drawable.popo20, R.drawable.popo21, R.drawable.popo22,
			R.drawable.popo23, R.drawable.popo24, R.drawable.popo25,
			R.drawable.popo26, R.drawable.popo27, R.drawable.popo28,
			R.drawable.popo29, R.drawable.popo30, R.drawable.popo31,
			R.drawable.popo32, R.drawable.popo33, R.drawable.popo34,
			R.drawable.popo35, R.drawable.popo36, R.drawable.popo37,
			R.drawable.popo38, R.drawable.popo39, R.drawable.popo40,
			R.drawable.popo41, R.drawable.popo42, R.drawable.popo43,
			R.drawable.popo44, R.drawable.popo45, R.drawable.popo46,
			R.drawable.popo47, R.drawable.popo48, R.drawable.popo49,
			R.drawable.popo50 };
}
